package com.crungoo;

import com.crungoo.entities.User;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.convert.support.DefaultConversionService;

/**
 * @author fujc-dev@qq.com
 * @description TODO
 * @date 2023/6/27 17:36
 */

public class 类型转化Programs {

	public static void main(String[] args) {

		//=========PropertyEditor=========
		// 类型转换器的使用
		StringToUserPropertyEditor propertyEditor = new StringToUserPropertyEditor();
		propertyEditor.setAsText("1");
		User value = (User) propertyEditor.getValue();
		System.out.println(value);

		// Spring容器使用
		//AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		//UserService userService = context.getBean(UserService.class);
		//userService.test();

		//=========ConversionService=========
		// 类型转化服务使用
		DefaultConversionService conversionService = new DefaultConversionService();
		conversionService.addConverter(new StringToUserConverter());
		User value1 = conversionService.convert("1", User.class);
		System.out.println(value1.getName());

		// Spring容器使用
		//AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		SimpleTypeConverter typeConverter = new SimpleTypeConverter();
		typeConverter.registerCustomEditor(User.class, new StringToUserPropertyEditor());
		//typeConverter.setConversionService(conversionService);
		User value2 = typeConverter.convertIfNecessary("1", User.class);
		System.out.println(value2.getName());

		//=========TypeConverter=========
	}
}
